package com.ruoyi.member.service.impl;

import com.ruoyi.member.domain.MemberOrder;
import com.ruoyi.member.mapper.MemberOrderMapper;
import com.ruoyi.member.service.IMemberOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;

/**
 * 会员订单Service业务层处理
 *
 * @author ruoyi
 * @date 2025-01-22
 */
@Service
public class MemberOrderServiceImpl implements IMemberOrderService {

    @Autowired
    private MemberOrderMapper memberOrderMapper;

    /**
     * 查询会员订单
     *
     * @param orderId 会员订单主键
     * @return 会员订单
     */
    @Override
    public MemberOrder selectMemberOrderById(Long orderId) {
        return memberOrderMapper.selectMemberOrderById(orderId);
    }

    /**
     * 查询会员订单列表
     *
     * @param memberOrder 会员订单
     * @return 会员订单集合
     */
    @Override
    public List<MemberOrder> selectMemberOrderList(MemberOrder memberOrder) {
        return memberOrderMapper.selectMemberOrderList(memberOrder);
    }

    /**
     * 新增会员订单
     *
     * @param memberOrder 会员订单
     * @return 结果
     */
    @Override
    public int insertMemberOrder(MemberOrder memberOrder) {
        memberOrder.setCreateTime(new Date());
        return memberOrderMapper.insertMemberOrder(memberOrder);
    }

    /**
     * 修改会员订单
     *
     * @param memberOrder 会员订单
     * @return 结果
     */
    @Override
    public int updateMemberOrder(MemberOrder memberOrder) {
        memberOrder.setUpdateTime(new Date());
        return memberOrderMapper.updateMemberOrder(memberOrder);
    }

    /**
     * 批量删除会员订单
     *
     * @param orderIds 需要删除的会员订单主键集合
     * @return 结果
     */
    @Override
    public int deleteMemberOrderByIds(Long[] orderIds) {
        return memberOrderMapper.deleteMemberOrderByIds(orderIds);
    }

    /**
     * 删除会员订单信息
     *
     * @param orderId 会员订单主键
     * @return 结果
     */
    @Override
    public int deleteMemberOrderById(Long orderId) {
        return memberOrderMapper.deleteMemberOrderById(orderId);
    }

    /**
     * 同步订单
     *
     * @param memberId 会员ID
     * @return 结果
     */
    @Override
    public int syncOrders(Long memberId) {
        // 这里实现订单同步逻辑
        // 例如：从第三方支付平台同步订单状态
        try {
            // 模拟同步操作
            List<MemberOrder> orders = memberOrderMapper.selectOrdersByMemberId(memberId);
            for (MemberOrder order : orders) {
                if ("0".equals(order.getStatus())) {
                    // 检查支付状态
                    // 更新订单状态
                    order.setStatus("1");
                    order.setPaidAt(new Date());
                    memberOrderMapper.updateMemberOrder(order);
                }
            }
            return orders.size();
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * 更新订单状态
     *
     * @param memberOrder 会员订单
     * @return 结果
     */
    @Override
    public int updateOrderStatus(MemberOrder memberOrder) {
        memberOrder.setUpdateTime(new Date());
        return memberOrderMapper.updateMemberOrder(memberOrder);
    }

    /**
     * 同步订单状态
     *
     * @param orderId 订单ID
     * @return 结果
     */
    @Override
    public int syncOrderStatus(Long orderId) {
        MemberOrder order = memberOrderMapper.selectMemberOrderById(orderId);
        if (order != null && "0".equals(order.getStatus())) {
            // 这里可以调用第三方支付接口查询订单状态
            // 模拟更新为已支付状态
            order.setStatus("1");
            order.setPaidAt(new Date());
            return memberOrderMapper.updateMemberOrder(order);
        }
        return 0;
    }

    /**
     * 同步待处理订单
     *
     * @return 结果
     */
    @Override
    public int syncPendingOrders() {
        MemberOrder query = new MemberOrder();
        query.setStatus("0"); // 待支付状态
        List<MemberOrder> pendingOrders = memberOrderMapper.selectMemberOrderList(query);

        int count = 0;
        for (MemberOrder order : pendingOrders) {
            // 检查订单创建时间，超过30分钟未支付的订单自动取消
            long createTime = order.getCreateTime().getTime();
            long currentTime = System.currentTimeMillis();
            if ((currentTime - createTime) > 30 * 60 * 1000) {
                order.setStatus("3"); // 已取消
                order.setUpdateTime(new Date());
                memberOrderMapper.updateMemberOrder(order);
                count++;
            }
        }
        return count;
    }
}